package com.example.blog.config;

import com.example.blog.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * ClassName: LoginInterceptor
 * Package: IntelliJ IDEA
 * Description:
 *
 * @Author 谷京京
 * @Create 2024/3/28 12:10
 * @Version 1.0
 */
@Slf4j
@Configuration
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //进行登录校验
        //从header中获取token
        //验证token
        String token = request.getHeader("user_token");
        log.info("从header中获取token" + token);
        Claims claims = JwtUtil.parseToken(token);
        if(claims == null) {
            response.setStatus(401);
            return false;
        }
        return true;
    }
}
